package com.ghy.controller;

import com.ghy.dto.CartInsertDTO;
import com.ghy.dto.CartPageDTO;
import com.ghy.dto.CartUpdateDTO;
import com.ghy.entity.Cart;
import com.ghy.vo.CartSimpleListVO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.ghy.entity.Cart;
import com.ghy.service.CartService;

import java.util.List;

/**
 * 购物车表 控制层。
 *
 * @author ghy
 * @since 2024-12-22
 */
@Tag(name = "购物车")
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary ="新增——单条",description ="新增一条购物车记录")
    @PostMapping("insert")
    public boolean insert(@Validated @RequestBody CartInsertDTO dto) {
        return cartService.insert(dto);
    }

    @Operation(summary ="查询——单条",description ="按主键查询一条购物车记录")
    @GetMapping("select/{id}")
    public Cart select(@PathVariable("id") Long id) {
        return cartService.select(id);
    }

    @Operation(summary = "查询——简单查询全部", description = "查询全部购物车记录")

    @GetMapping("simpleList")
    public List<CartSimpleListVO> simpleList() {
        return cartService.simpleList();
    }


    @Operation(summary ="查询——分页",description ="分页查询购物车记录")
    @GetMapping("page")
    public Page<Cart> page(@Validated CartPageDTO dto) {
        return cartService.page(dto);
    }

    @Operation(summary ="查询——总计金额",description ="按用户主键查询")
    @GetMapping("totalAmountByUserId/{id}")
    public Double totalAmountByUserId(@PathVariable("id") Long id){
        return cartService.totalAmountByUserId(id);
    }

    @Operation(summary ="删除——清空记录",description ="按用户主键查询")
    @DeleteMapping("cleanByUserId/{id}")
    public boolean cleanByUserId(@PathVariable("id") Long id){
        return cartService.cleanByUserId(id);
    }

    @Operation(summary="修改——单条",description="按主键修改一条购物车记录")
    @PutMapping("update")
    public boolean update(@RequestBody CartUpdateDTO dto) {
        return cartService.update(dto);
    }

    @Operation(summary ="删除——单条",description ="按主键删除一条购物车记录")
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return cartService.delete(id);
    }

    @Operation(summary="删除——批量",description ="按主键批量删除购物车记录")
    @DeleteMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("ids") List<Long> ids) {
        return cartService.deleteBatch(ids);
    }

    @Operation(summary = "删除 - 清空记录", description = "按用户主键清空该用户的购物车记录")
    @DeleteMapping("clearByUserId/{userId}")
    public boolean clearByUserId(@PathVariable("userId") Long userId) {
        return cartService.clearByUserId(userId);
    }



}
